<?php

use com\gdl\newagk\interfaces\LabelsServiceIf;
use com\gdl\common\dataobject\common\ResultDO;
use com\gdl\newagk\dataobject\labels\LabelsDTO;
use com\gdl\newagk\dataobject\labels\LabelsRequestDTO;


class api_LabelsService extends api_Abstract implements LabelsServiceIf
{
	
	//标签列表
    public function labeList(LabelsDTO $labels)
    {
		$result = new ResultDO();

		$reg = $labels->reg ? $labels->reg : '';
		$paperid = $labels->id ? $labels->id : 0;
		try{
			$result->code = 0;
			$result->success = true;
            if($reg==''){
				 $result->message = "缺少参数";
				 return $result;
			}
			$Labels = new model_newagk_labels();
			$paper = new model_newagk_paper();
			$chooseid = $paper->getInfoArgLabelId($paperid);
			$list = $Labels->getInfoArg(['types'=>1,'pid'=>0],'id,name','order by ords,id desc');//'admin_reg'=>$reg,
			$result->data = $list;
			if($list){
				foreach($list as $v){
					$datas[] = [
								'id'=>$v['id'],
								'name'=>$v['name'],
								'pick'=>empty($chooseid[$v['id']]) ? 0 : 1,
								];
				}
				$result->data = $datas;
				$result->code = 1;
			} else {
				$result->code = 0;
			}
		
            
            return $result;
        } catch (Exception $e) {
            $result->success = false;
            $result->code = $e->getCode();
            $result->message = $e->getMessage();
        }
        return $result;
    }
	
	
	
	
	//添加标签
    public function add(LabelsDTO $labels)
    {
		$result = new ResultDO();
		$filename = $labels->filename ? $labels->filename : [];
		$title = $labels->title ? $labels->title : '';
		$reg = $labels->reg ? $labels->reg : '';
		$ords = $labels->id ? $labels->id : 0;
		try{
			$result->code = 0;
			$result->success = true;
            if($title==''){
				 $result->message = "请填写标签名";
				 return $result;
			}
			$Labels = new model_newagk_labels();
			$father = $Labels->insert(['name'=>$title,'ords'=>$ords,'admin_reg'=>$reg]);
			if($father>0){} else {
				 $result->message = "添加失败";
				 return $result;
			}
			foreach($filename as $v){
					$datas=[
							'name'=>$v['name'],
							'pid'=>$father,
							'admin_reg'=>$reg,
							'ords'=>0,
							];
					$Labels->insert($datas);
			}
			$result->message = '成功';
            $result->code = 1;
            return $result;
        } catch (Exception $e) {
            $result->success = false;
            $result->code = $e->getCode();
            $result->message = $e->getMessage();
        }
        return $result;
    }
	
	//查看标签详情
	public function details(LabelsDTO $labels)
    {
		$result = new ResultDO();
		$reg = $labels->reg ? $labels->reg : '';
		$id = $labels->id ? $labels->id : 0;
		$dwid = $labels->field ? explode(',',$labels->field) : [];
		try{
			$result->code = 0;
			$result->success = true;
			if(empty($dwid)){
				if(!$id){
					 $result->message = "缺少参数";
					 return $result;
				}
				$Labels = new model_newagk_labels();
				$title = $Labels->selectOne(['id'=>$id,'types'=>1],'id,name,ords');//,'admin_reg'=>$reg
				 if(empty($title)){
					 $result->message = "数据不存在";
					 return $result;
				}
				$tag = $Labels->getInfoArg(['pid'=>$id,'types'=>1],'id,name');//,'admin_reg'=>$reg
				$datas = ['id'=>$title['id'],'title'=>$title['name'],'tag'=>[],'ords'=>$title['ords']];
				foreach($tag as $v){
					$datas['tag'][]=['id'=>$v['id'],'name'=>$v['name'],];
				}
				$datas['tag'] = json_encode($datas['tag']);
			} else {
				$id = [];
				foreach($dwid as $v){
					$id[] = intval($v);
				}
				$Labels = new model_newagk_labels();

				$tag = $Labels->batchList($id);
				
				$datas['tag'] = json_encode($tag['tag']);
				$datas['tagSelect'] = json_encode($tag['tagSelect']);
			}
			$result->data[] = $datas;
			$result->message = '成功';
            $result->code = 1;
            return $result;
        } catch (Exception $e) {
            $result->success = false;
            $result->code = $e->getCode();
            $result->message = $e->getMessage();
        }
        return $result;
    }
	
	
	//修改标签
    public function edit(LabelsDTO $labels)
    {
		$result = new ResultDO();
		$filename = $labels->filename ? $labels->filename : [];
		$title = $labels->title ? $labels->title : '';
		$reg = $labels->reg ? $labels->reg : '';
		$ords = $labels->code ? $labels->code : 0;
		$id = $labels->id ? $labels->id : 0;
		try{
			$result->code = 0;
			$result->success = true;
            if($title==''){
				 $result->message = "请填写标签名";
				 return $result;
			}
			$Labels = new model_newagk_labels();
			$father = $Labels->update(['id'=>$id],['name'=>$title,'ords'=>$ords]);
			$orgId = [];//'admin_reg'=>$reg,
			foreach($filename as $v){
					
					$datas=[
							'name'=>$v['name'],
							'pid'=>$id,
							'admin_reg'=>$reg,
							'ords'=>0,
							];
					if($v['id']){
						$orgId[] = $v['id'];
						$Labels->update(['id'=>$v['id']],['name'=>$v['name']]);//'admin_reg'=>$reg,
					} else {
						$lid = $Labels->insert($datas);
						$orgId[] = $lid;
						
					}
			}

			if(!empty($orgId)){
				$Labels->delEmpty($orgId,$id,$reg); //去掉已经不存在的列
			}
			$result->message ='成功';
            $result->code = 1;
            return $result;
        } catch (Exception $e) {
            $result->success = false;
            $result->code = $e->getCode();
            $result->message = $e->getMessage();
        }
        return $result;
    }
	
	
	
	//删除标签
    public function delOut(LabelsDTO $labels)
    {
		
		$result = new ResultDO();
		$reg = $labels->reg ? $labels->reg : '';
		$id = $labels->id ? $labels->id : 0;
		try{
			$result->code = 0;
			$result->success = true;
            if($reg=='' || !$id){
				 $result->message = "请填写标签名";
				 return $result;
			}
			$Labels = new model_newagk_labels();
			$Labels->delete(['id'=>$id,'types'=>1]);//'admin_reg'=>$reg,
			$Labels->delete(['pid'=>$id,'types'=>1]);//'admin_reg'=>$reg,
			$result->message ='成功';
            $result->code = 1;
            return $result;
        } catch (Exception $e) {
            $result->success = false;
            $result->code = $e->getCode();
            $result->message = $e->getMessage();
        }
        return $result;
    }
}
